找传奇、传世资源到传世资源站!

三菱FX PLC 串口通讯(开关量输入/输出)示例源码

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

c#实现开关量输入
from clipboard
from clipboardusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace WindowsFormsApplication1{ public partial class Form1 : Form { string data; SolidBrush bush1 = new SolidBrush(Color.Red); SolidBrush bush2 = new SolidBrush(Color.Green); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { serialPort1.Close(); Close(); } private void Form1_Load(object sender, EventArgs e) { serialPort1.PortName = "COM1"; serialPort1.BaudRate = 9600; serialPort1.DataBits = 7; serialPort1.Open(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (serialPort1.IsOpen) serialPort1.Close(); } private void timer1_Tick(object sender, EventArgs e) { Graphics g1 = this.pictureBox1.CreateGraphics(); Graphics g2 = this.pictureBox2.CreateGraphics(); Graphics g3 = this.pictureBox3.CreateGraphics(); Graphics g4 = this.pictureBox4.CreateGraphics(); Graphics g5 = this.pictureBox5.CreateGraphics(); Graphics g6 = this.pictureBox6.CreateGraphics(); Graphics g7= this.pictureBox7.CreateGraphics(); Graphics g8= this.pictureBox8.CreateGraphics(); Graphics g9 = this.pictureBox9.CreateGraphics(); Graphics g10 = this.pictureBox10.CreateGraphics(); Graphics g11 = this.pictureBox11.CreateGraphics(); Graphics g12 = this.pictureBox12.CreateGraphics(); Graphics g13 = this.pictureBox13.CreateGraphics(); Graphics g14 = this.pictureBox14.CreateGraphics(); Graphics g15= this.pictureBox15.CreateGraphics(); Graphics g16 = this.pictureBox16.CreateGraphics(); data = serialPort1.ReadExisting(); int b = data.Length; if (b == 68) { if (data.Substring(2, 1) == "1") g1.FillEllipse(bush1, 0, 0, 20, 20); else g1.FillEllipse(bush2, 0, 0, 20, 20); if (data.Substring(6, 1) == "1") g2.FillEllipse(bush1, 0, 0, 20, 20); else g2.FillEllipse(bush2, 0, 0, 20, 20); if (data.Substring(10, 1) == "1") g3.FillEllipse(bush1, 0, 0, 20, 20); else g3.FillEllipse(bush2, 0, 0, 20, 20); if (data.Substring(14, 1) == "1") g4.FillEllipse(bush1, 0, 0, 20, 20); else g4.FillEllipse(bush2, 0, 0, 20, 20); if (data.Substring(18, 1) == "1") g5.FillEllipse(bush1, 0, 0, 20, 20); else g5.FillEllipse(bush2, 0, 0, 20, 20); if (data.Substring(22, 1) == "1") g6.FillEllipse(bush1, 0, 0, 20, 20); else g6.FillEllipse(bush2, 0, 0, 20, 20); if (data.Substring(26, 1) == "1") g7.FillEllipse(bush1, 0, 0, 20, 20); else g7.FillEllipse(bush2, 0, 0, 20, 20); if (data.Substring(30, 1) == "1") g8.FillEllipse(bush1, 0, 0, 20, 20); else g8.FillEllipse(bush2, 0, 0, 20, 20); if (data.Substring(34, 1) == "1") g9.FillEllipse(bush1, 0, 0, 20, 20); else g9.FillEllipse(bush2, 0, 0, 20, 20); if (data.Substring(38, 1) == "1") g10.FillEllipse(bush1, 0, 0, 20, 20); else g10.FillEllipse(bush2, 0, 0, 20, 20); if (data.Substring(42, 1) == "1") g11.FillEllipse(bush1, 0, 0, 20, 20); else g11.FillEllipse(bush2, 0, 0, 20, 20); if (data.Substring(46, 1) == "1") g12.FillEllipse(bush1, 0, 0, 20, 20); else g12.FillEllipse(bush2, 0, 0, 20, 20); if (data.Substring(50, 1) == "1") g13.FillEllipse(bush1, 0, 0, 20, 20); else g13.FillEllipse(bush2, 0, 0, 20, 20); if (data.Substring(54, 1) == "1") g14.FillEllipse(bush1, 0, 0, 20, 20); else g14.FillEllipse(bush2, 0, 0, 20, 20); if (data.Substring(58, 1) == "1") g15.FillEllipse(bush1, 0, 0, 20, 20); else g15.FillEllipse(bush2, 0, 0, 20, 20); if (data.Substring(62, 1) == "1") g16.FillEllipse(bush1, 0, 0, 20, 20); else g16.FillEllipse(bush2, 0, 0, 20, 20); } } private void button2_Click(object sender, EventArgs e) { //向PLC发送指令:02 30 31 30 30 30 32 30 03 35 36,功能是从D0开始读取32个字节数据 char[] c = { (char)2, (char)48, (char)49, (char)48, (char)48, (char)48, (char)50, (char)48, (char)3, (char)53, (char)54 }; int i = 0; string s = ""; while (i<11) { s=s c[i].ToString (); i ; } serialPort1 .Write (s); timer1.Enabled = true; } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复